package controller

import (
	"hft/Database/admin"
	"hft/Http/Models/request"
	services "hft/Http/Services"
	utils "hft/Http/Utils"
	"hft/tools"
	"strconv"

	"github.com/gin-gonic/gin"
)

type HomeSwiper struct{}

func (*HomeSwiper) GetArticleList(c *gin.Context) {

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	list, err := services.HomeSwiperService.GeArticleList(claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(list, c)

}

func (*HomeSwiper) CreateSwiper(c *gin.Context) {

	var data *admin.HomeSwiper

	c.ShouldBindJSON(&data)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.HomeSwiperService.CreateSwiper(claims.BaseClaims, data)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

func (*HomeSwiper) GetSwiperList(c *gin.Context) {

	origin, _ := strconv.Atoi(c.DefaultQuery("origin", "-1"))

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	data, err := services.HomeSwiperService.GetSwiper(claims.BaseClaims, origin)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)

}

func (*HomeSwiper) Delete(c *gin.Context) {

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	var ids *request.IDS

	c.ShouldBindJSON(&ids)

	err = services.HomeSwiperService.Delete(ids.IDS, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

var HomeSwiperController = new(HomeSwiper)
